package student.aistar.service.impl;

import student.aistar.dao.ITVideoDao;
import student.aistar.dao.impl.TVideoDaoImpl;
import student.aistar.entity.TVideo;
import student.aistar.model.dto.PageDTO;
import student.aistar.service.IVideoService;

import java.util.ArrayList;
import java.util.List;

public class VideoServiceImpl implements IVideoService {
    ITVideoDao itVideoDao = new TVideoDaoImpl();

//    @Override
//    public List<TVideo> findPageList(String username, String title, Integer pageNow, Integer pageSize) {
//        int rows = itVideoDao.countVideos(username, title);
//        List<TVideo> list = new ArrayList<>();
//        list = itVideoDao.findPage(username, title, pageNow, pageSize);
//        if(rows % pageSize == 0)
//            System.out.println("需要分: " + rows/pageSize + "页");
//        else
//            System.out.println("需要分: " + (rows/pageSize + 1));
//        return list;
//    }

    @Override
    public PageDTO<TVideo> findPageList(String username, String title, Integer pageNow, Integer pageSize) {
        PageDTO<TVideo> pageDTO = new PageDTO<>();
        pageNow = (pageNow==null?1:pageNow);
        pageSize = (pageSize==null?2:pageSize);
        int rows = itVideoDao.countVideos(username, title);
        pageDTO.setPageNow(pageNow);
        pageDTO.setPageSize(pageSize);
        pageDTO.setPageCount(rows/pageSize);

        int pageCount = rows/pageSize;
        if(rows % pageSize != 0)
            pageCount++;
        List<TVideo> list = itVideoDao.findPage(username, title, pageNow, pageSize);
        if(list != null)
            pageDTO.setDataList(list);
        return pageDTO;
    }
}
